﻿using B.S.Meeting.Domain;
using B.S.Meeting.ErrorCode;
using B.S.Meeting.Infrastructure.Interfaces;
using B.S.Meeting.WriteAPI.Applications.Commands.Approves;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace B.S.Meeting.WriteAPI.Applications.CommandHandlers.Approves
{
    public class UpdApproveCommandHandler : IRequestHandler<UpdApproveCommand, APIResult<int>>
    {
        IBaseRepository<ApproveReservation> aRep;
        ILogger<UpdApproveCommandHandler> logger;

        public UpdApproveCommandHandler(IBaseRepository<ApproveReservation> aRep, ILogger<UpdApproveCommandHandler> logger)
        {
            this.aRep = aRep;
            this.logger = logger;
        }

        public async Task<APIResult<int>> Handle(UpdApproveCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var list = await aRep.GetAll().Where(x => x.ApproveReservationId == request.ApproveReservationId && x.MeetingReservationId==request.MeetingReservationId).FirstOrDefaultAsync();
                list.ApproveReservationStatus = request.ApproveReservationStatus;
                list.ApproveReservationName = request.ApproveReservationName;
                list.ApproveReservationOpinion = request.ApproveReservationOpinion;
                await aRep.UpdAsync(list);
                return new APIResult<int>
                {
                    Code = StatusEnumCode.Ok,
                    Message = "审批成功",
                    Data = 1
                };
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
